/*
 * @Date: 2023-04-13 13:58:55
 * @LastEditors: Dannycjh 2233879049@qq.com
 * @LastEditTime: 2023-04-15 12:12:09
 */
import { director } from "cc";
import { Singleton } from "../common/Common";

export default class EventManager extends Singleton<EventManager> {

    /**
     * 全局事件
     */
    public static event: EventTarget = null;

    constructor() {
        super();
        this.initialize();
    }
    
    /**
    * 初始化
    */
    private initialize(): void {
        EventManager.event = new EventTarget();
    }


    /**
    * 派发全局事件
    */
    emit(key: string, data1?: any, data2?: any, data3?: any, data4?: any) {
        director.emit(key, data1, data2, data3, data4);
    }
    /**
    * 监听全局事件
    */
    on(key: string, callback: any, target?: any) {
        director.on(key, callback, target);
    }

    /**
    * 移除全局事件
    */
    off(key: string, callback?: any, target?: any) {
        director.off(key, callback, target);
    }

    /**
     * 移除指定对象的所有事件
     * @param target 
     */
    removeAll(target ?: any) {
        director.targetOff(target);
    }

}